<?php
/**
 * Created by PhpStorm.
 * User: lingyun
 * Date: 2018/1/15
 * Time: 18:03
 * Desc: 门店广告管理
 */
namespace app\store\controller;

use controller\BasicAdmin;

use service\DataService;

use think\Db;

class Advert extends BasicAdmin{
    /**
     * @Author  lingyun
     * @Desc    广告列表
     */
    public function advert_list(){
        $this->title = '广告管理';
        $get = $this->request->get();
        $store_id = session('user')['store_id'];
        $join = [
            ['ys_goods b', 'a.goods_id=b.id', 'LEFT'],
            ['ys_advert_position c','a.adp_id=c.adp_id','LEFT']
        ];
        $db = Db::name('store_advert_list')->alias('a')->where('a.store_id', $store_id)->field('a.*,b.name,c.title as position')->where('a.is_deleted', 0)->join($join);
        foreach (['title'] as $key) {
            (isset($get[$key]) && $get[$key] !== '') && $db->whereLike('a.title', "%{$get[$key]}%");
        }
        $qiniu = config('qiniu');
        $this->assign('yangshi', $qiniu['yangshi']);          //七牛样式后缀
        return parent::_list($db);
    }

    /**
     * @Author  lingyun
     * @Desc    添加 -- 编辑广告
     */
    public function add_edit_advert(){
        $request = request();
        if($request->isPost()){
            $post = input('post.');
            if(empty($post['id'])){
                $post['addtime'] = time();
                $res = db('store_advert_list')->data($post)->insert();
                $res>0?$this->success('保存成功',''):$this->error('保存失败','');
            }else{
                $res = db('store_advert_list')->update($post);
                $res!==false?$this->success('保存成功',''):$this->error('保存失败','');
            }
        }
        $id = input('id');
        $store_id = session('user')['store_id'];
        //广告信息
        $advert = db('store_advert_list')->where('id',$id)->find();
        //商品
        $where_goods = [
            'a.is_deleted'=>0,
            'a.onsale'=>1,
            'a.store_id'=>$store_id
        ];
        $join = [
            ['ys_goods b','a.goods_id=b.id','LEFT']
        ];
        //门店没商品
        $goods = db('store_goods_stock')->alias('a')->join($join)->where($where_goods)->field('b.id,b.logo,b.name')->select();
        $where_advert_position = [
            'status'=>1,
            'is_deleted'=>0
        ];
        $advert_position = db('advert_position')->where($where_advert_position)->select();
        $this->assign('advert',$advert);
        $this->assign('goods',$goods);
        $this->assign('store_id',$store_id);
        $this->assign('advert_position',$advert_position);
        return $this->fetch('add_edit_advert');
    }

    /**
     * @Author  lingyun
     * @Desc    设置门店广告状态
     */
    public function set_advert_status(){
        $post = input('post.');
        $res = db('store_advert_list')->where('id',$post['id'])->update([$post['field']=>$post['value']]);
        $res!==false?$this->success('设置成功',''):$this->error('设置失败','');
    }

    /**
     * @Author  lingyun
     * @Desc    删除广告
     */
    public function del_advert(){
        $table = 'StoreAdvertList';
        if (DataService::update($table)) {
            $this->success("删除成功！", '');
        }
        $this->error("删除失败，请稍候再试！");
    }

    /**
     * @Author  lingyun
     * @Desc    轮播图
     */
    public function banner(){
        $this->title = '广告管理';
        $get = $this->request->get();
        $store_id = session('user')['store_id'];
        $join = [
            ['ys_goods b', 'a.goods_id=b.id', 'LEFT'],
            ['ys_banner_position c','a.bap_id=c.bap_id','LEFT']
        ];
        $where_banner = [
            'a.store_id'=>$store_id,
            'a.is_deleted'=>0
        ];
        $db = Db::name('store_banner')->alias('a')->where($where_banner)->field('a.*,b.name,c.title as position')->join($join);
        foreach (['title'] as $key) {
            (isset($get[$key]) && $get[$key] !== '') && $db->whereLike('a.title', "%{$get[$key]}%");
        }
        $qiniu = config('qiniu');
        $this->assign('yangshi', $qiniu['yangshi']);          //七牛样式后缀
        return parent::_list($db);
    }

    /**
     * @Author  lingyun
     * @Desc    添加编辑轮播图
     */
    public function add_edit_banner(){
        $request = request();
        if($request->isPost()){
            $post = input('post.');
            if(empty($post['id'])){
                $post['addtime'] = time();
                $res = db('store_banner')->data($post)->insert();
                $res>0?$this->success('添加成功',''):$this->error('保存失败','');
            }else{
                $res = db('store_banner')->data($post)->update();
                $res!==false?$this->success('保存成功',''):$this->error('保存失败','');
            }
        }
        $id = input('id');      //轮播图id
        $store_id = session('user')['store_id'];

        $banner = db('store_banner')->where('id',$id)->find();
        $join = [
            ['ys_goods b','a.goods_id=b.id','LEFT']
        ];
        $where_goods = [
            'a.is_deleted'=>0,
            'a.onsale'=>1,
            'a.store_id'=>$store_id
        ];
        //推荐商品
        $goods = db('store_goods_stock')->alias('a')->join($join)->where($where_goods)->field('b.id,b.logo,b.name')->select();
        $where_banner_position = [
            'status'=>1,
            'is_deleted'=>0
        ];
        //门店轮播图位置
        $banner_position = db('banner_position')->where($where_banner_position)->select();
        $this->assign('goods',$goods);
        $this->assign('banner',$banner);
        $this->assign('store_id',$store_id);
        $this->assign('banner_position',$banner_position);
        return $this->fetch('add_edit_banner');
    }

    /**
     * @Author  lingyun
     * @Desc    设置轮播图状态
     */
    public function set_banner_status(){
        $post = input('post.');
        $res = db('store_banner')->where('id',$post['id'])->update([$post['field']=>$post['value']]);
        $res!==false?$this->success('设置成功',''):$this->error('设置失败','');
    }

    /**
     * @Author  lingyun
     * @Desc    删除轮播图
     */
    public function del_banner(){
        $table = 'StoreBanner';
        if (DataService::update($table)) {
            $this->success("删除成功！", '');
        }
        $this->error("删除失败，请稍候再试！");
    }
}